package customerEnter.Frame.JDBCDao.SqlProcessing.SqlServer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import customerEnter.Frame.DaoDataTransmission.DaoFramUpdateOrAddMap;
import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileParameter;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameSqlProcessingInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileParameterInterface;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.javaReflexClass;
import customerEnter.tool.WhetherToEmpty;

/**
 * 类描述：處理 添加的語句
 * 
 * 类名称：DaoFrameSqlProcessingInsertImpl
 * 
 * 创建人：dy
 * 
 * 创建时间：2017-7-12 下午3:50:39
 * 
 */
public class DaoFrameSqlProcessingInsertImpl implements DaoFrameSqlProcessingInterface<String> {

	@Override
	public <T, K> String SqlProcessing(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		try {
			Object[] objects = null;
			SqlPrecompileInterface sqlPrecompileInterface = new SqlPrecompileParameter();
			if (daoFrameDMLBaseTransmission.isIsPreparedStatementParameter()) {
				objects = daoFrameDMLBaseTransmission.getObjects();
				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getDML(), "", "", daoFrameDMLBaseTransmission.getTableName(), "");
			} else {
				objects = setInsertMappingSql(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getMappingObject(),
						daoFramePameTransmission);
			}
			for (int i = 0; i < objects.length; i++) {
				sqlPrecompileInterface.AddNumberValue(i + 1, objects[i]);
			}

			daoFramePameTransmission
					.setSqlPrecompileParameterInterface((SqlPrecompileParameterInterface) sqlPrecompileInterface);
		} catch (Exception e) {
			Log4j.setLog4j(getClass());
			Log4j.Log4JOutError(e.getMessage());
		}
		return daoFramePameTransmission.SQL;
	}

	/**
	 * 增加數據的方法
	 * 
	 * @param <T>
	 * @param <K>
	 * 
	 * @throws ReflectiveOperationException
	 * @throws IllegalArgumentException
	 */
	public <T, K> Object[] setInsertMappingSql(String DataBase, String TableName, Object t,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws Exception {
		DaoFramUpdateOrAddMap daoFramUpdateOrAddMap = javaReflexClass.getObjectNamesAndValuesAdd(t);
		String fieldNames = "";
		String values = "";
		List<Object> list = new ArrayList<Object>();

		for (Entry<String, Object> entry : daoFramUpdateOrAddMap.getData().entrySet()) {
			if (!WhetherToEmpty.StringWhetherToEmpty(fieldNames)) {
				fieldNames += ",";
				values += ",";
			}
			fieldNames += entry.getKey();
			values += "?";
			list.add(entry.getValue());
		}
		daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(DataBase, " insert ", "", "into", TableName + "("
				+ fieldNames + ")", "values(" + values + " ) ");
		return list.toArray();
	}

}
